package com.web.controller;

import com.github.pagehelper.PageInfo;
import com.web.entity.Result;
import com.web.entity.SportRecord;
import com.web.service.SportRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/sportRecord")
@CrossOrigin
public class SportRecordController {

    @Autowired
    private SportRecordService sportRecordService;

    //http://localhost:8080/sportRecord/getByUserIdAndDate?userId=1&recordDate=2025-09-18
    @GetMapping("/getByUserIdAndDate")
    public Result getByUserIdAndDate(SportRecord sportRecord){
        log.info("查询结果：{}", sportRecord);
        List<SportRecord> byUserIdAndDate = sportRecordService.getByUserIdAndDate(sportRecord);
        return Result.success(byUserIdAndDate);
    }




    //http://localhost:8080/sportRecord/list?page=1&pageSize=10
    /**
     * 分页条件查询所有数据
     * @param page
     * @param pageSize
     * @param sportRecord
     * @return
     */
    @GetMapping("/list")
    public Result list(Integer page, Integer pageSize, SportRecord sportRecord){
        log.info("分页查询运动记录");
        PageInfo<SportRecord> list = this.sportRecordService.list(page, pageSize, sportRecord);
        return Result.success(list);
    }

    //http://localhost:8080/sportRecord/findById?sportId=1

    /**
     * 根据id查询
     * @param sportId
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer sportId){
        log.info("查询运动记录");
        SportRecord sportRecord = this.sportRecordService.findById(sportId);
        return Result.success(sportRecord);
    }

    //http://localhost:8080/sportRecord/add

    /**
     * 添加数据
     * @param sportRecord
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody SportRecord sportRecord){
        log.info("添加运动记录");
        this.sportRecordService.add(sportRecord);
        return Result.success();
    }

    //http://localhost:8080/sportRecord/update

    /**
     * 修改
     * @param sportRecord
     * @return
     */
    @PutMapping("/update/{sportId}")
    public Result update(@PathVariable Integer sportId,@RequestBody SportRecord sportRecord){
        log.info("更新运动记录");
        this.sportRecordService.update(sportId,sportRecord);
        return Result.success();
    }

    //http://localhost:8080/sportRecord/delete/1

    /**
     * 删除
     * @param sportId
     * @return
     */
    @DeleteMapping("/delete/{sportId}")
    public Result delete(@PathVariable Integer sportId){
        log.info("删除运动记录");
        this.sportRecordService.delete(sportId);
        return Result.success();
    }

    //http://localhost:8080/sportRecord/byUserId?userId=1

    /**
     * 查询用户运动记录
     * @param userId
     * @return
     */
    @GetMapping("/findUserId")
    public Result byUserId(Integer userId){
        log.info("查询用户运动记录");
        PageInfo<SportRecord> list = this.sportRecordService.byUserId(1, 10, userId);
        return Result.success(list);
    }


    //http://localhost:8080/sportRecord/byDoctorId?doctorId=1

    /**
     * 根据医生查询记录
     * @param doctorId
     * @return
     */
    @GetMapping("/findDoctorId")
    public Result byDoctorId(Integer doctorId){
        log.info("查询医生运动记录");
        PageInfo<SportRecord> list = this.sportRecordService.byDoctorId(1, 10, doctorId);
        return Result.success(list);
    }



}
